﻿/*	@file	r_math_matrix34.cpp
	@brief	行列
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "r_math_matrix34.h"
#include "r_math_def.h"
#include "r_math_matrix33.h"
#include "r_math_matrix44.h"
#include "r_math_vector2.h"
#include "r_math_vector3.h"
#include "r_math_vector4.h"
#include <math.h>
#include <string.h>

namespace r{
	namespace math{
		Matrix34 :: Matrix34 ( void ) {}
		Matrix34 :: Matrix34 ( r_f32 val )
		{
			_11 = _12 = _13 = _21 = _22 = _23 = _31 = _32 = _33 = _41 = _42 = _43 = val;
		}
		Matrix34 :: Matrix34 (
			r_f32 s11, r_f32 s12, r_f32 s13,
			r_f32 s21, r_f32 s22, r_f32 s23,
			r_f32 s31, r_f32 s32, r_f32 s33,
			r_f32 s41, r_f32 s42, r_f32 s43 )
		{
			_11 = s11;
			_12 = s12;
			_13 = s13;
			_21 = s21;
			_22 = s22;
			_23 = s23;
			_31 = s31;
			_32 = s32;
			_33 = s33;
			_41 = s41;
			_42 = s42;
			_43 = s43;
		}
		Matrix34 :: Matrix34 ( const r_f32 *sv )
		{
			_11 = sv[0];
			_12 = sv[1];
			_13 = sv[2];
			_21 = sv[3];
			_22 = sv[4];
			_23 = sv[5];
			_31 = sv[6];
			_32 = sv[7];
			_33 = sv[8];
			_41 = sv[9];
			_42 = sv[10];
			_43 = sv[11];
		}
		Matrix34 :: Matrix34 ( const r::math::Matrix33 &rot, const r::math::Vector3D &pos )
		{
			_11 = rot._11;
			_12 = rot._12;
			_13 = rot._13;
			_21 = rot._21;
			_22 = rot._22;
			_23 = rot._23;
			_31 = rot._31;
			_32 = rot._32;
			_33 = rot._33;
			_41 = pos.x;
			_42 = pos.y;
			_43 = pos.z;
		}

		void	Matrix34 :: identity ( void )
		{
			_11 = _22 = _33 = 1;
			_12 = _13 =
					  _21 = _23 =
								_31 = _32 =
										  _41 = _42 = _43 = 0;
		}

		void	Matrix34 :: aim ( const r::math::Vector3D &position, const r::math::Vector3D &target, const r::math::Vector3D &upvector )
		{
			r::math::Vector3D zaxis = r::math::Vector3D ( target - position ).normalize();
			r::math::Vector3D xaxis = r::math::Vector3D ( upvector ).cross ( zaxis ).normalize();
			r::math::Vector3D yaxis = r::math::Vector3D ( zaxis ).cross ( xaxis );

			_11 = xaxis.x;
			_12 = xaxis.y;
			_13 = xaxis.z;

			_21 = yaxis.x;
			_22 = yaxis.y;
			_23 = yaxis.z;

			_31 = zaxis.x;
			_32 = zaxis.y;
			_33 = zaxis.z;

			_41 = position.x;
			_42 = position.y;
			_43 = position.z;
		}

		Matrix34	Matrix34 :: operator * ( const Matrix34 &src ) const
		{
			Matrix34 tmat (
				_11 * src._11 + _12 * src._21 + _13 * src._31,
				_11 * src._12 + _12 * src._22 + _13 * src._32,
				_11 * src._13 + _12 * src._23 + _13 * src._33,

				_21 * src._11 + _22 * src._21 + _23 * src._31,
				_21 * src._12 + _22 * src._22 + _23 * src._32,
				_21 * src._13 + _22 * src._23 + _23 * src._33,

				_31 * src._11 + _32 * src._21 + _33 * src._31,
				_31 * src._12 + _32 * src._22 + _33 * src._32,
				_31 * src._13 + _32 * src._23 + _33 * src._33,

				_41 * src._11 + _42 * src._21 + _43 * src._31,
				_41 * src._12 + _42 * src._22 + _43 * src._32,
				_41 * src._13 + _42 * src._23 + _43 * src._33 );
			return tmat;
		}
		const Matrix34&	Matrix34 :: operator *= ( const Matrix34 &src )
		{
			*this = ( *this ) * src;
			return *this;
		}

		Matrix34	Matrix34 :: operator * ( r_f32 src ) const
		{
			Matrix34 tmat (
				_11 * src,	_12 * src, _13 * src,
				_21 * src,	_22 * src, _23 * src,
				_31 * src,	_32 * src, _33 * src,
				_41 * src,	_42 * src, _43 * src
			);
			return tmat;
		}

		const Matrix34&	Matrix34 :: operator *= ( r_f32 src )
		{
			*this = ( *this ) * src;
			return *this;
		}

		Matrix34	Matrix34 :: operator + ( const Matrix34 &src ) const
		{
			Matrix34 tmat (
				_11 + src._11,	_12 + src._12, _13 + src._13,
				_21 + src._21,	_22 + src._22, _23 + src._23,
				_31 + src._31,	_32 + src._32, _33 + src._33,
				_41 + src._41,	_42 + src._42, _43 + src._43
			);
			return tmat;
		}

		const Matrix34&	Matrix34 :: operator += ( const Matrix34 &src )
		{
			*this = ( *this ) + src;
			return *this;
		}

		void	Matrix34 :: transposeInverse ( void )
		{
			Matrix34 tmat ( *this );
		//	_11 = tmat._11;
			_12 = tmat._21;
			_13 = tmat._31;
			_21 = tmat._12;
		//	_22 = tmat._22;
			_23 = tmat._32;
			_31 = tmat._13;
			_32 = tmat._23;
		//	_33 = tmat._33;
			_41 = - ( tmat._41 * tmat._11 + tmat._42 * tmat._12 + tmat._43 * tmat._13 );
			_42 = - ( tmat._41 * tmat._21 + tmat._42 * tmat._22 + tmat._43 * tmat._23 );
			_43 = - ( tmat._41 * tmat._31 + tmat._42 * tmat._32 + tmat._43 * tmat._33 );
		}

		void	Matrix34 :: transpose ( Matrix44& dst )
		{
			dst = Matrix44 ( _11, _21, _31, _41, _12, _22, _32, _42, _13, _23, _33, _43, 0, 0, 0, 1 );
		}

		const Matrix34&	Matrix34 :: translate ( const r::math::Vector3D &src )
		{
			_11 = _22 = _33 = 1;
			_12 = _13 = _21 = _23 = _31 = _32 = 0;
			_41 = src.x;
			_42 = src.y;
			_43 = src.z;
			return *this;
		}

		const Matrix34&	Matrix34 :: setTranslate ( const r::math::Vector3D &src )
		{
			_41 = src.x;
			_42 = src.y;
			_43 = src.z;
			return *this;
		}

		const Matrix34&	Matrix34 :: scale ( const r::math::Vector3D &src )
		{
			_12 = _13 =
					  _21 = _23 =
								_31 = _32 =
										  _41 = _42 = _43 = 0;
			_11 = src.x;
			_22 = src.y;
			_33 = src.z;
			return *this;
		}

		const Matrix34&	Matrix34 :: scale ( r_f32 src )
		{
			_12 = _13 =
					  _21 = _23 =
								_31 = _32 =
										  _41 = _42 = _43 = 0;
			_11 = _22 = _33 = src;
			return *this;
		}

		const Matrix34&	Matrix34 :: rotateX ( r_f32 src )
		{
			_11 = _12 = _13 =
							_21 =
								_31 =
									_41 = _42 = _43 = 0;
			_22 = _33 = cos ( src );
			_23 = sin ( src );
			_32 = -sin ( src );
			return *this;
		}

		const Matrix34&	Matrix34 :: rotateY ( r_f32 src )
		{
			_12 =
				_21 = _23 =
						  _32 =
							  _41 = _42 = _43 = 0;
			_11 = _33 = cos ( src );
			_13 = -sin ( src );
			_31 = sin ( src );
			_22 = 1;
			return *this;
		}

		const Matrix34&	Matrix34 :: rotateZ ( r_f32 src )
		{
			_13 =
				_23 =
					_31 = _32 =
							  _41 = _42 = _43 = 0;
			_11 = _22 = cos ( src );
			_12 = sin ( src );
			_21 = -sin ( src );
			_33 = 1;
			return *this;
		}

		const Matrix34&	Matrix34 :: rotateAxis ( const r::math::Vector3D &vec, r_f32 rot )
		{
			r_f32 s = sin ( rot );
			r_f32 c = cos ( rot );
			r_f32 nc = 1.0f - c;
			r_f32 sx = vec.x * s;
			r_f32 sy = vec.y * s;
			r_f32 sz = vec.z * s;
			r_f32 nx = vec.x * nc;
			r_f32 ny = vec.y * nc;
			r_f32 nz = vec.z * nc;
			_11 = vec.x * nx + c;
			_12 = vec.x * ny - sz;
			_13 = vec.x * nz + sy;
			_21 = vec.y * nx + sz;
			_22 = vec.y * ny + c;
			_23 = vec.y * nz - sx;
			_31 = vec.z * nx - sy;
			_32 = vec.z * ny + sx;
			_33 = vec.z * nz + c;
			_41 = _42 = _43 = 0;
			return *this;
		}

		Matrix34 :: operator r::math::Matrix33 ( void ) const
		{
			r::math::Matrix33 tmat ( f );
			return tmat;
		}

		Matrix34 :: operator Matrix44 ( void ) const
		{
			Matrix44 tmat (
				_11, _12, _13, 0,
				_21, _22, _23, 0,
				_31, _32, _33, 0,
				_41, _42, _43, 1 );
			return tmat;
		}

		const Matrix34&	Matrix34 :: setRotateAxis ( const r::math::Matrix33 &rot )
		{
			_11 = rot._11;
			_12 = rot._12;
			_13 = rot._13;
			_21 = rot._21;
			_22 = rot._22;
			_23 = rot._23;
			_31 = rot._31;
			_32 = rot._32;
			_33 = rot._33;
			return *this;
		}

		r::math::Vector3D	Matrix34 :: transform3D ( const r::math::Vector2D &vec ) const
		{
			return r::math::Vector3D (
					   vec.x * _11 + vec.y * _21 + _41,
					   vec.x * _12 + vec.y * _22 + _42,
					   vec.x * _13 + vec.y * _23 + _43 );
		}
		r::math::Vector3D	Matrix34 :: transform3D ( const r::math::Vector3D &vec ) const
		{
			return r::math::Vector3D (
					   vec.x * _11 + vec.y * _21 + vec.z * _31 + _41,
					   vec.x * _12 + vec.y * _22 + vec.z * _32 + _42,
					   vec.x * _13 + vec.y * _23 + vec.z * _33 + _43 );
		}
		r::math::Vector3D	Matrix34 :: transform3D33 ( const r::math::Vector3D &vec ) const
		{
			return r::math::Vector3D (
					   vec.x * _11 + vec.y * _21 + vec.z * _31,
					   vec.x * _12 + vec.y * _22 + vec.z * _32,
					   vec.x * _13 + vec.y * _23 + vec.z * _33 );
		}
		r::math::Vector4D	Matrix34 :: transform4D ( const r::math::Vector3D &vec ) const
		{
			return r::math::Vector4D (
					   vec.x * _11 + vec.y * _21 + vec.z * _31 + _41,
					   vec.x * _12 + vec.y * _22 + vec.z * _32 + _42,
					   vec.x * _13 + vec.y * _23 + vec.z * _33 + _43,
					   1 );
		}
		r::math::Vector4D	Matrix34 :: transform4D ( const r::math::Vector4D &vec ) const
		{
			return r::math::Vector4D (
					   vec.x * _11 + vec.y * _21 + vec.z * _31 + vec.w * _41,
					   vec.x * _12 + vec.y * _22 + vec.z * _32 + vec.w * _42,
					   vec.x * _13 + vec.y * _23 + vec.z * _33 + vec.w * _43,
					   1 );
		}
		const Matrix34&	Matrix34 :: normalize ( void )
		{
			r::math::Vector3D v1 = r::math::Vector3D ( &_11 ).normalize();
			r::math::Vector3D v2 = r::math::Vector3D ( &_21 ).normalize();
			r::math::Vector3D v3 = r::math::Vector3D ( &_31 ).normalize();
			_11 = v1.x;
			_12 = v1.y;
			_13 = v1.z;
			_21 = v2.x;
			_22 = v2.y;
			_23 = v2.z;
			_31 = v3.x;
			_32 = v3.y;
			_33 = v3.z;
			return *this;
		}

		r::math::Vector3D	Matrix34 :: getSize ( void ) const
		{
			return r::math::Vector3D ( r::math::Vector3D ( &_11 ).length(), r::math::Vector3D ( &_21 ).length(), r::math::Vector3D ( &_31 ).length() );
		}

		r::math::Vector3D	Matrix34 :: EulerXYZ ( void ) const
		{
			r::math::Matrix33 mat = *this;
			return mat.EulerXYZ();
		}

		r::math::Vector3D	Matrix34 :: EulerXZY ( void ) const
		{
			r::math::Matrix33 mat = *this;
			return mat.EulerXZY();
		}

		r::math::Vector3D	Matrix34 :: EulerYXZ ( void ) const
		{
			r::math::Matrix33 mat = *this;
			return mat.EulerYXZ();
		}

		r::math::Vector3D	Matrix34 :: EulerYZX ( void ) const
		{
			r::math::Matrix33 mat = *this;
			return mat.EulerYZX();
		}

		r::math::Vector3D	Matrix34 :: EulerZXY ( void ) const
		{
			r::math::Matrix33 mat = *this;
			return mat.EulerZXY();
		}

		r::math::Vector3D	Matrix34 :: EulerZYX ( void ) const
		{
			r::math::Matrix33 mat = *this;
			return mat.EulerZYX();
		}

		const Matrix34&	Matrix34 :: rotateXYZ ( const r::math::Vector3D &rot )
		{
			Matrix34 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotX * rotY * rotZ;
			return *this;
		}

		const Matrix34&	Matrix34 :: rotateXZY ( const r::math::Vector3D &rot )
		{
			Matrix34 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotX * rotZ * rotY;
			return *this;
		}

		const Matrix34&	Matrix34 :: rotateYXZ ( const r::math::Vector3D &rot )
		{
			Matrix34 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotY * rotX * rotZ;
			return *this;
		}
		const Matrix34&	Matrix34 :: rotateYZX ( const r::math::Vector3D &rot )
		{
			Matrix34 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotY * rotZ * rotX;
			return *this;
		}
		const Matrix34&	Matrix34 :: rotateZXY ( const r::math::Vector3D &rot )
		{
			Matrix34 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotZ * rotX * rotY;
			return *this;
		}
		const Matrix34&	Matrix34 :: rotateZYX ( const r::math::Vector3D &rot )
		{
			Matrix34 rotX, rotY, rotZ;
			rotX.rotateX ( rot.x );
			rotY.rotateY ( rot.y );
			rotZ.rotateZ ( rot.z );
			*this = rotZ * rotY * rotX;
			return *this;
		}
	}
}

